﻿using JV.Com.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace JV.Com.Business
{
    public class Cache
    {
        private static object m_lockInstance = new object();
        private static Cache s_instance;
        
        public static Cache Instance
        {
            get
            {
                if (s_instance == null && !s_instance.m_isInitialized)
                    throw new Exception("Cache must be initialized before used");
                return s_instance;
            }
        }

        private bool m_isInitialized;
        private Cache()
        {
            m_isInitialized = false;
        }

        public static async Task InitAsync()
        {
            s_instance = new Cache();
            await s_instance.LoadMachinesAsync();
            s_instance.m_isInitialized = true;
        }

        public bool IsInitialied { get { return m_isInitialized; } }

        private Dictionary<int, Machine> m_machines;
        public Dictionary<int, Machine> Machines { get { return m_machines; } }
        private async Task LoadMachinesAsync()
        {
            m_machines = await InternalService.GetMachines();
        }
    }
}
